<template>
  <div class="read-container">
    <div class="read-top">
      <span>编辑书籍</span>
      <el-button class="confirm" @click="confirm">确定</el-button>
    </div>
    <div class="read-content">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="120px"
        class="demo-ruleForm"
      >
        <el-form-item label="书籍名称：" prop="book_name">
          <el-input v-model="ruleForm.book_name"></el-input>
        </el-form-item>
        <el-form-item label="作者名称：" prop="book_author">
          <el-input v-model="ruleForm.book_author"></el-input>
        </el-form-item>
        <el-form-item label="设定年龄：" prop="book_age_id">
          <el-select v-model="ruleForm.book_age_id" placeholder="请选择" @change="selectedAge">
            <el-option
              v-for="item in optionsAge"
              :key="item.id"
              :label="item.read_age"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分类：" prop="book_type_id">
          <el-select v-model="ruleForm.book_type_id" placeholder="请选择" @change="selectedType">
            <el-option
              v-for="item in optionsType"
              :key="item.id"
              :label="item.type_name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="内容简介：" prop="book_desc">
          <el-input type="textarea" v-model="ruleForm.book_desc"></el-input>
        </el-form-item>
        <el-form-item label="推荐理由：" prop="recommend_reason">
          <el-input type="textarea" v-model="ruleForm.recommend_reason"></el-input>
        </el-form-item>
        <el-form-item label="分级阅读：" prop="classification_read">
          <el-input v-model="ruleForm.classification_read"></el-input>
        </el-form-item>
        <el-form-item label="书籍详情头图：" prop="gradingRead">
          <el-upload
            class="avatar-uploader"
            action="http://yaya.izhixiu.cn/api/common/upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :on-error="handleAvatarError"
            :before-upload="beforeAvatarUpload"
            :data="photoParam"
          >
            <el-button class="button uploadButton">上传图片</el-button>
          </el-upload>
          <div class="avatar-uploader-img">
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-picture avatar-uploader-icon"></i>
          </div>
        </el-form-item>
        <el-form-item label="其他描述：" prop="other_description">
          <Ueditor ref="ue"></Ueditor>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import Ueditor from "@/common/editor/editor";
import { Message } from "element-ui";

export default {
  props: {
    optionsType: {
      type: Array,
      default: () => []
    },
    optionsAge: {
      type: Array,
      default: () => []
    },
    ruleFormParams: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      photoParam: {},
      imageUrl: "",
      ruleForm: {
        book_name: "",
        book_author: "",
        book_age_id: "",
        book_type_id: "",
        book_desc: "",
        recommend_reason: "",
        classification_read: "",
        book_cover: "",
        other_description: ""
      },
      rules: {
        book_name: [
          { required: true, message: "请输入书籍名称", trigger: "blur" }
        ],
        book_author: [
          { required: true, message: "请输入作者名称", trigger: "blur" }
        ],
        book_age_id: [
          { required: true, message: "请选择年龄", trigger: "change" }
        ],
        book_type_id: [
          { required: true, message: "请选择类型", trigger: "change" }
        ],
        book_desc: [
          { required: true, message: "请输入内容简介", trigger: "blur" }
        ],
        recommend_reason: [
          { required: true, message: "请输入推荐理由", trigger: "blur" }
        ],
        classification_read: [
          { required: true, message: "请输入分级阅读", trigger: "blur" }
        ],
        book_cover: [
          { required: true, message: "请上传图片", trigger: "blur" }
        ],
        other_description: [
          { required: true, message: "请输入其他信息", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    selectedType(val) {
      // this.optionsType.findIndex((item) => {
      //   return item
      // })
      this.ruleForm.book_type_id = val
    },
    selectedAge(val) {
      this.ruleForm.book_age_id = val;
      // console.log(val)
    },
    confirm() {
      let content = this.$refs.ue.content;
      this.ruleForm.other_description = content;
      this.$emit("confirmSend", this.ruleForm);
      this.$router.push({ path: "/read/readRecommend" });
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = res.data.photo;
      this.ruleForm.book_cover = res.data.photo;
      console.log(this.ruleForm);
    },
    handleAvatarError(err) {
      Message({
        message: "文件格式不正确!",
        type: "error"
      });
    },
    beforeAvatarUpload(file) {
      // console.log(file)
      this.photoParam.photo = file;
    },
    
  },
  watch: {
    ruleFormParams(newVal, oldVal) {
      Object.assign(this.ruleForm, {book_id: newVal.book_id, book_name: newVal.book_name, book_author: newVal.book_author, book_age_id: newVal.book_age_id, book_type_id: newVal.book_type_id, book_desc: newVal.book_desc, recommend_reason: newVal.recommend_reason, classification_read: newVal.classification_read, book_cover: newVal.book_cover, other_description: newVal.other_description})
      this.imageUrl = newVal.book_cover
      this.$refs.ue.content = newVal.other_description
    }
  },
  components: {
    Ueditor
  }
};
</script>
<style lang="scss" scoped>
.read-container {
  padding: 20px;
  padding-top: 60px;
  .read-top {
    width: 100%;
    height: 89px;
    display: flex;
    justify-content: space-between;
    .confirm {
      width: 87px;
      height: 30px;
      line-height: 20px;
      border-radius: 6px;
      background-color: rgba(27, 166, 135, 1);
      text-align: center;
      padding: 0;
      color: #fff;
    }
  }
  .read-content {
    .el-form-item /deep/ .el-input__inner {
      width: 467px;
      height: 36px;
      .el-select {
        width: 100%;
      }
    }
    .el-form-item /deep/ .el-textarea__inner {
      width: 583px;
      height: 83px;
    }
    .button {
      width: 120px;
      height: 38px;
      line-height: 38px;
      border-radius: 4px;
      background-color: rgba(246, 246, 246, 1);
      text-align: center;
      border: 1px solid rgba(233, 233, 233, 1);
      padding: 0;
      color: #444;
    }
  }
  .read-content /deep/.el-upload {
    margin-top: 12px;
  }
  // .avatar-uploader {
  //   position: relative;
  //   .uploadButton {
  //     position: absolute;
  //   }
  // }

  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 120px;
    height: 120px;
    line-height: 120px;
    text-align: center;
    background: #ccc;
  }
  .avatar {
    width: 120px;
    height: 120px;
    display: block;
  }
}
</style>